package com.michale.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.constants.UserConstant;
import com.michale.common.domain.system.DictTypeEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.system.service.DictTypeService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Michale
 * @apiNote 字典类型管理
 */
@Api(tags = "字典类型管理")
@RestController
@RequestMapping("system/dict/type")
public class DictTypeController extends BaseController {

    @Resource
    private DictTypeService dictTypeService;

    /**
     * 分页查询-字典类型列表
     *
     * @param noticeEntity 分页查询条件
     * @return 字典类型列表
     */
    @GetMapping("/dictTypeList")
    @PreAuthorize("@security.isPermission('system:dict:list')")
    public PageResult getDictTypeList(DictTypeEntity noticeEntity) {
        startPage();
        List<DictTypeEntity> dictTypeList = dictTypeService.selectDictTypeList(noticeEntity);
        PageInfo<DictTypeEntity> pageInfo = new PageInfo<>(dictTypeList);
        return pageResult(pageInfo);
    }

    /**
     * 查询字典类型详细
     *
     * @param dictTypeId 字典类型ID
     * @return 字典类型详细
     */
    @GetMapping("/{dictTypeId}")
    @PreAuthorize("@security.isPermission('system:dict:query')")
    public AjaxResult getDictInfo(@PathVariable Long dictTypeId) {
        DictTypeEntity noticeList = dictTypeService.selectDictTypeById(dictTypeId);
        return AjaxResult.success(noticeList);
    }

    /**
     * 新增参数配置
     *
     * @param noticeList 字典类型信息
     * @return 结果
     */
    @PostMapping("/insertDictType")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('system:dict:add')")
    public AjaxResult insertDictType(@Validated @RequestBody DictTypeEntity noticeList) {

        if (UserConstant.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(noticeList))) {
            return AjaxResult.error("新增字典'" + noticeList.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.insertDictType(noticeList));
    }

    /**
     * 修改字典类型信息
     *
     * @param noticeList 字典类型信息
     * @return 结果
     */
    @PutMapping("/updateDict")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:dict:edit')")
    public AjaxResult updateDict(@Validated @RequestBody DictTypeEntity noticeList) {

        if (UserConstant.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(noticeList))) {
            return AjaxResult.error("修改字典'" + noticeList.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.updateDictType(noticeList));
    }

    /**
     * 根据字典类型ID删除字典类型信息
     *
     * @param dictTypeIds 需要删除的字典类型ID
     * @return 结果
     */
    @DeleteMapping("/deleteDict/{dictTypeIds}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('system:dict:romove')")
    public AjaxResult deleteDict(@PathVariable Long[] dictTypeIds) {
        dictTypeService.deleteDictTypeByIds(dictTypeIds);
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionSelect")
    public AjaxResult optionSelect() {
        return success(dictTypeService.selectDictTypeAll());
    }

    /**
     * 刷新字典缓存
     */
    @DeleteMapping("/refreshCache")
    @PreAuthorize("@security.isPermission('system:dict:romove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    public AjaxResult refreshCache() {
        dictTypeService.resetDictCache();
        return success();
    }
}
